package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import pojo.FllowCase;

import common.RowMapper;
import common.SQLTemplate;

import dao.FllowCaseDAO;

public class FllowCaseDAOImpl extends BaseDAO implements FllowCaseDAO {

	@Override
	public List<FllowCase> getFllowCasesByCustomerId(String customerId) {
		String sql = SQLTemplate.FLLOW_CASE_SELECT_JOIN_CUSTOMER_OPERATOR_SQL
				+ "AND CUSTOMER_ID='" + customerId + "';";
		List<FllowCase> fllowCase = null;
		try {
			fllowCase = (List<FllowCase>) queryForList(sql,
					new FllowCaseRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return fllowCase;
	}

	@Override
	public List<FllowCase> getFllowCasesByOperatorId(String operatorId) {
		String sql = SQLTemplate.FLLOW_CASE_SELECT_JOIN_CUSTOMER_OPERATOR_SQL
				+ "AND OPERATOR_ID='" + operatorId + "';";
		List<FllowCase> fllowCase = null;
		try {
			fllowCase = (List<FllowCase>) queryForList(sql,
					new FllowCaseRowMapper());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return fllowCase;
	}

	@Override
	public FllowCase add(FllowCase fllowCase) {
		String tableName = "T_FLLOW_CASE";
		String[] columnNames = { "RECORD", "NOTE", "CUSTOMER_ID", "OPERATOR_ID" };
		String[] values = { fllowCase.getRecord(), fllowCase.getNote(),
				fllowCase.getCustomerId(), fllowCase.getOperatorId() };
		String sql = SQLTemplate.addSQLTemplate(tableName, columnNames, values);
		try {
			insert(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return fllowCase;
	}

	@Override
	public FllowCase update(FllowCase fllowCase) {
		String tableName = "T_FLLOW_CASE";
		String[] columnNames = { "RECORD", "NOTE", "CUSTOMER_ID", "OPERATOR_ID" };
		String[] values = { fllowCase.getRecord(), fllowCase.getNote(),
				fllowCase.getCustomerId(), fllowCase.getOperatorId() };
		String sql = SQLTemplate.updateSQLTemplate(tableName, columnNames,
				values) + "WHERE ID='" + fllowCase.getId();
		try {
			update(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return fllowCase;
	}

	private class FllowCaseRowMapper implements RowMapper<Object> {
		private FllowCaseRowMapper() {
		}

		public FllowCase mapRow(ResultSet rs, int rowNum) throws SQLException {
			String id = rs.getString("id");
			String record = rs.getString("record");
			String note = rs.getString("note");
			String time = rs.getString("time");
			String customerId = rs.getString("customer_id");
			String customerName = rs.getString("customer_name");
			String operatorId = rs.getString("operator_id");
			String operatorName = rs.getString("operator_name");
			FllowCase fllowcase = new FllowCase();
			if (id != null) {
				fllowcase.setId(id);
			}
			if (record != null) {
				fllowcase.setRecord(record);
			}
			if (note != null) {
				fllowcase.setNote(note);
			}
			if (time != null) {
				fllowcase.setTime(time);
			}
			if (customerId != null) {
				fllowcase.setCustomerId(customerId);
			}
			if (customerName != null) {
				customerName = customerName.replaceAll("\\[\\w+\\]", "");
				fllowcase.setCustomerName(customerName);
			}
			if (operatorId != null) {
				fllowcase.setOperatorId(operatorId);
			}
			if (operatorName != null) {
				fllowcase.setOperatorName(operatorName);
			}
			return fllowcase;
		}
	}
}
